var Zan = require('../../dist/index');

const app = getApp()
var config = require('../../config.js')
const util = require("../../utils/util.js")
let loginReadyCallBack;
let logined;

Page(Object.assign({}, Zan.NoticeBar, Zan.Dialog, {
  data: {
    shouldShow: false,
    news: [],
    projects: [],
    movable: {
      text: '通知：清明节放假安排 清明节是悼念先人、缅怀先烈、祭奠逝者、对亲人表达怀念之情的传统节日,体现了中华民族的传统美德。'
    },
    imgUrls: [
      '/images/img1.jpg',
      '/images/img2.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    userInfo: {},
    icons: [{
        navigateUrl: '../gzrz/myAuditList/myAuditList',
        url: '/icons/工作日志.jpg',
        text: '工作日志',
        msg_count: 1
      }, {
        cls: 'edit',
        url: '/icons/施工档案.jpg',
        text: '施工档案',
        click: 'createCar',
        msg_count: 12
      },
      {
        navigateUrl: '../clsg/goodList/index',
        url: '/icons/材料申购.jpg',
        text: '材料申购',
        click: 'clsgClick',
        msg_count: 5
      },
      {
        cls: 'chat',
        url: '/icons/工地库.jpg',
        text: '工地库',
        click: 'openMap',
        msg_count: 120
      }, {
        cls: 'upgrade',
        url: '/icons/未标题-1.jpg',
        text: '扣款单',
        msg_count: 0
      }, {
        cls: 'location',
        url: '/icons/借.jpg',
        text: '借款单',
        click: 'locate',
        msg_count: 0
      }, {
        cls: 'gold-coin',
        url: '/icons/罚款单.jpg',
        text: '罚款单',
        msg_count: 0
      }, {
        cls: 'clock',
        url: '/icons/请款单.jpg',
        text: '请款单',
        msg_count: 0
      }, {
        cls: 'clock',
        url: '/icons/奖励单.jpg',
        text: '奖励单',
        msg_count: 0
      },
      {
        cls: 'close',
        url: '/icons/oa.jpg',
        text: 'OA',
        click: 'scanCode',
        msg_count: 0
      }
    ],
    cases: [
      '/images/timg.jpeg',
      '/images/timg-2.jpeg',
      '/images/timg-3.jpeg',
      '/images/timg-4.jpeg'
    ]
  },
  onLoad: function() {
    this.setData({
      shouldShow: false
    })
    wx.showLoading({
      title: '加载中..',
    })
    wx.hideTabBar({

    })
    let that = this
    this.login()
    // if (app.globalData.openid) {
    //   that.login()
    // }
    // else {
    //   app.openIdReadyCallBack = function (openid) {
    //     that.login()
    //   }
    // }//界面加载就登陆

  },
  login() {
    let that = this
    wx.request({
      url: config.loginUrl,
      method: "POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        __ajax: true,
        mobileLogin: true,
        openID: "testopenid"
      },
      success: res => {
        logined = true
        if (res.data.success) {
          wx.setStorageSync("JSESSIONID", res.data.body.JSESSIONID)
          app.globalData.loginResult = res.data.body

          that.setData({
            shouldShow: true,
            userInfo: res.data.body
          })
          wx.hideLoading()
          wx.showTabBar({

          })
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '需要用户绑定账号',
            success: function(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../personalCenter/login/index',
                })
              } else if (res.cancel) {
                wx.navigateTo({
                  url: '../personalCenter/login/index',
                })
              }
            }
          })
        }
      }
    })
    util.postData(config.logAuditListUrl, {
        preMinId: '2018-1-1',
        projectNo: 'CN201806001'
        // projectName:'接口测试项目'

      })
      .then(res => {
        console.log(res.body.data)
        res.body.data.forEach(x => {
          x.updateDate = util.formatDateCN(x.updateDate)
        })
        this.setData({
          projects: res.body.data
        })
      })
  },
  onShow() {
    // 在方法中传入对应的 componentId
    this.initZanNoticeBarScroll('movable');
    //this.loadNews();
  },
  scanCode() {
    wx.scanCode({
      // onlyFromCamera: true,
      scanType: ['qrCode', 'barCode', 'pdf417', 'datamatrix'],
      success: function(res) {
        wx.showModal({
          title: '扫描结果',
          content: res.result,
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  getMyLocation(cb) {
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        if (cb) {
          cb(res)
        }
      }
    })
  },
  locate() {
    this.getMyLocation(function(res) {
      wx.showModal({
        title: '当前经纬度',
        content: '经度:' + res.longitude + ';纬度:' + res.latitude,
      })
    })
  },
  openMap() {
    this.getMyLocation(function(res) {
      wx.openLocation({
        latitude: res.latitude,
        longitude: res.longitude,
      })
    })
  },
  createCar() {
    wx.navigateTo({
      url: '../cars/index?mode=add',
    })
  },
  clsgClick() {
    wx.navigateTo({
      url: '../clsg/goods/goods',
    })
  },
  loadNews() {
    var that = this
    wx.request({
      url: urlConfig.getCarList,
      data: {
        searchkind: 1
      },
      success: res => {
        that.setData({
          news: res.data
        })
      },
      fail: res => {
        console.log(res)
      }
    })
  }
}));